﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Prism.Events;
using Prism.Regions;
using SoonCode.Core;
using SoonCode.FocusTest.Events;
using SoonCode.FocusTest.ViewModels;

namespace SoonCode.FocusTest.Views
{
    /// <summary>
    /// TestWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TestWindow : BaseWindow
    {
        private IEventAggregator aggregator;
        private SubscriptionToken eventToken;
        private IRegionManager regionManager;
        private string regionName = "testRegion";
        private TeacherWindow teacherWindow;
        public TestWindow(IEventAggregator aggregator)
        {
            this.aggregator = aggregator;
            this.regionManager = (new RegionManager()).CreateRegionManager();
            RegionManager.SetRegionManager(this, this.regionManager);
            this.regionManager.RegisterViewWithRegion<SelectMember>(regionName);
            InitializeComponent();
            this.Loaded += TestWindow_Loaded;
            this.Closing += TestWindow_Closing;
        }


        private void TestWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void TestWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.regionManager.RequestNavigate(regionName,nameof(SelectMember));
            this.eventToken = aggregator.GetEvent<StartTestEvent>().Subscribe(ShowTeacherWindow);
        }

        public void ShowTeacherWindow()
        {
            if(teacherWindow==null)
                teacherWindow = new TeacherWindow(this.aggregator);
            ScreenManager.Instance.Show(this.Owner, teacherWindow);
            this.Close();
        }
        private void BtnClose_OnClick(object sender, RoutedEventArgs e)
        {
            this.aggregator.GetEvent<CloseTestEvent>().Publish();
            this.Close();
        }
    }
}
